/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Telas;

import Beans.BeanAgendamento;
import Beans.BeanPessoa;
import Beans.BeanPlanoSaude;
import CRUD.CRUDAgendamento;
import CRUD.CRUDPessoa;
import CRUD.CRUDPlanoSaude;
import ClassesExtras.ButtonTabComponent;
import ClassesExtras.TextAreaMaiuscula;
import Conexao.ConexaoBanco;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Marcos
 */
public class Jp_CadastroAgendamentoSecretaria extends javax.swing.JPanel {

    DefaultTableModel TMAgendamento = new DefaultTableModel(null, new String[]{"Agendamento", "Agendado_Para:",
        "Horário", "Status", "Médico", "Paciente", "Plano de Saúde", "Telefone", "Nascimento",
        "Idade", "Sexo", "Cidade", "Endereço", "Observação"}) {// Faz a sobreposição de método e permite tbm a instancia de classe abstrata, quando implementado seus métodos
        @Override
        public boolean isCellEditable(int row, int column) {
            return false; //Não deixar editar uma linha da JTable
//            return super.isCellEditable(row, column);
        }
    };
    List<BeanPessoa> ListaPessoa;
    List<BeanAgendamento> ListaAgendamento;
    List<BeanPessoa> ListaPaciente;
    List<BeanPlanoSaude> ListaPlanoSaude;
    String nom_paciente;
    public Connection conexao;

    /**
     * Creates new form Jp_CadastroAgendamentoSecretaria
     */
    public Jp_CadastroAgendamentoSecretaria() throws SQLException {
        initComponents();
        AlinhaColunaJTable();
        jComboBoxStatus.setEnabled(false);
        jTableAgendamento.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // Selecionar somente uma linha da JTable
        jTabbedPaneAgendamentoSecretaria.setEnabledAt(1, false);
        try {
            listaPaciente();
            listaMedico();
            listaagendamento();
            listaPlanoSaude();
        } catch (Exception ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        }
        jTextAreaObservacoes.setLineWrap(true);
        this.conexao = ConexaoBanco.getConexao();
        run();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelCadastroAgendamento = new javax.swing.JPanel();
        jTabbedPaneAgendamentoSecretaria = new javax.swing.JTabbedPane();
        jPanelAgendamento = new javax.swing.JPanel();
        jLabelStatus = new javax.swing.JLabel();
        jLabelPaciente = new javax.swing.JLabel();
        jComboBoxMedico = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaObservacoes = new TextAreaMaiuscula();
        jComboBoxStatus = new javax.swing.JComboBox();
        jComboBoxPaciente = new javax.swing.JComboBox();
        jLabelMedico = new javax.swing.JLabel();
        jButtonConsultaPaciente = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableAgendamento = new javax.swing.JTable();
        jLabelAgendamento = new javax.swing.JLabel();
        jTextFieldCodMedico = new javax.swing.JTextField();
        jTextFieldCodPaciente = new javax.swing.JTextField();
        jLabelObservacoes = new javax.swing.JLabel();
        jButtonConsultaMedico = new javax.swing.JButton();
        jTextFieldAgendamento = new javax.swing.JTextField();
        jButtonAlterar = new javax.swing.JButton();
        jButtonIncluir = new javax.swing.JButton();
        jLabelHorario = new javax.swing.JLabel();
        jFormattedTextFieldHorario = new javax.swing.JFormattedTextField();
        jLabelCamposObrigatorios = new javax.swing.JLabel();
        jButtonNovo = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabelMae = new javax.swing.JLabel();
        jLabelPai = new javax.swing.JLabel();
        jLabelEnd = new javax.swing.JLabel();
        jLabelTel = new javax.swing.JLabel();
        jLabelCPF = new javax.swing.JLabel();
        jLabelCel = new javax.swing.JLabel();
        jLabelNomMae = new javax.swing.JLabel();
        jLabelNomPai = new javax.swing.JLabel();
        jLabelDenEnd = new javax.swing.JLabel();
        jLabelNumTel = new javax.swing.JLabel();
        jLabelNumCel = new javax.swing.JLabel();
        jLabelNumCPF = new javax.swing.JLabel();
        jLabelNomPaciente = new javax.swing.JLabel();
        jLabelPlanoSaude = new javax.swing.JLabel();
        jComboBoxPlanoSaude = new javax.swing.JComboBox();
        jTextFieldCodPlanoSaude = new javax.swing.JTextField();
        jButtonConsultaPlanoSaude = new javax.swing.JButton();
        jLabelTipoAgendamento = new javax.swing.JLabel();
        jComboBoxTipoAgendamento = new javax.swing.JComboBox();
        jCalendarAgendamento = new com.toedter.calendar.JCalendar();
        jPanelPagamento = new javax.swing.JPanel();
        jTextFieldAgendamentoPagamento = new javax.swing.JTextField();
        jLabelAgendamentoPagamento = new javax.swing.JLabel();
        jLabelPacientePagamento = new javax.swing.JLabel();
        jTextFieldPacientePagamento = new javax.swing.JTextField();
        jTextFieldCodPacientePagamento = new javax.swing.JTextField();

        jPanelCadastroAgendamento.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabelStatus.setText("*Status:");

        jLabelPaciente.setText("*Paciente:");

        jComboBoxMedico.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxMedico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxMedicoActionPerformed(evt);
            }
        });

        jTextAreaObservacoes.setColumns(20);
        jTextAreaObservacoes.setRows(5);
        jScrollPane2.setViewportView(jTextAreaObservacoes);

        jComboBoxStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Em Aberto", "Em Atendimento", "Cancelado" }));

        jComboBoxPaciente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxPacienteActionPerformed(evt);
            }
        });

        jLabelMedico.setText("*Médico:");

        jButtonConsultaPaciente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Consultar.png"))); // NOI18N
        jButtonConsultaPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConsultaPacienteActionPerformed(evt);
            }
        });

        jTableAgendamento.setModel(TMAgendamento);
        jTableAgendamento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableAgendamentoMouseClicked(evt);
            }
        });
        jTableAgendamento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTableAgendamentoKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTableAgendamento);

        jLabelAgendamento.setText("Agendamento:");

        jTextFieldCodMedico.setEnabled(false);

        jTextFieldCodPaciente.setEnabled(false);

        jLabelObservacoes.setText("Observações");

        jButtonConsultaMedico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Consultar.png"))); // NOI18N
        jButtonConsultaMedico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConsultaMedicoActionPerformed(evt);
            }
        });

        jTextFieldAgendamento.setEnabled(false);

        jButtonAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Alterar.png"))); // NOI18N
        jButtonAlterar.setMnemonic('A');
        jButtonAlterar.setText("Alterar");
        jButtonAlterar.setToolTipText("Alterar");
        jButtonAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAlterarActionPerformed(evt);
            }
        });

        jButtonIncluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Incluir.png"))); // NOI18N
        jButtonIncluir.setMnemonic('I');
        jButtonIncluir.setText("Incluir");
        jButtonIncluir.setToolTipText("Incluir");
        jButtonIncluir.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jButtonIncluirMouseDragged(evt);
            }
        });
        jButtonIncluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonIncluirActionPerformed(evt);
            }
        });

        jLabelHorario.setText("*Horário:");

        try {
            jFormattedTextFieldHorario.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabelCamposObrigatorios.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabelCamposObrigatorios.setText("* Campos obrigatórios a serem preenchidos");

        jButtonNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Novo.png"))); // NOI18N
        jButtonNovo.setMnemonic('m');
        jButtonNovo.setText("Novo");
        jButtonNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNovoActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações do Paciente"));

        jLabelMae.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabelMae.setText("Mãe:");

        jLabelPai.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabelPai.setText("Pai:");

        jLabelEnd.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabelEnd.setText("End.:");

        jLabelTel.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabelTel.setText("Tel.:");

        jLabelCPF.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabelCPF.setText("CPF:");

        jLabelCel.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabelCel.setText("Cel.:");

        jLabelNomPaciente.setFont(new java.awt.Font("sansserif", 3, 14)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabelMae, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelPai)
                            .addComponent(jLabelEnd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelTel)
                            .addComponent(jLabelCPF)
                            .addComponent(jLabelCel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelNomPai, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelDenEnd, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                                    .addComponent(jLabelNumTel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabelNumCel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabelNumCPF, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabelNomMae, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addContainerGap())))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelNomPaciente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jLabelNomPaciente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelMae, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelNomMae, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelPai)
                    .addComponent(jLabelNomPai, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabelEnd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabelDenEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabelTel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabelNumTel, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabelCel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabelNumCel, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCPF)
                    .addComponent(jLabelNumCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jLabelPlanoSaude.setText("*Plano de Saúde:");

        jComboBoxPlanoSaude.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxPlanoSaude.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxPlanoSaudeActionPerformed(evt);
            }
        });

        jTextFieldCodPlanoSaude.setEnabled(false);

        jButtonConsultaPlanoSaude.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Consultar.png"))); // NOI18N

        jLabelTipoAgendamento.setText("*Tipo de agendamento:");

        jComboBoxTipoAgendamento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Consulta", "Reconsulta" }));

        jCalendarAgendamento.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jCalendarAgendamentoPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout jPanelAgendamentoLayout = new javax.swing.GroupLayout(jPanelAgendamento);
        jPanelAgendamento.setLayout(jPanelAgendamentoLayout);
        jPanelAgendamentoLayout.setHorizontalGroup(
            jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1235, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelAgendamentoLayout.createSequentialGroup()
                        .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelAgendamentoLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jCalendarAgendamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelCamposObrigatorios)
                                    .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                                        .addComponent(jButtonNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButtonIncluir, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButtonAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 428, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabelPaciente)
                                    .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabelObservacoes, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelAgendamentoLayout.createSequentialGroup()
                                            .addGap(16, 16, 16)
                                            .addComponent(jLabelAgendamento)))
                                    .addComponent(jLabelMedico)
                                    .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabelHorario)
                                        .addComponent(jLabelStatus))
                                    .addComponent(jLabelPlanoSaude))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                                        .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jTextFieldAgendamento, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jComboBoxStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                                                    .addComponent(jFormattedTextFieldHorario, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addGap(24, 24, 24)
                                                    .addComponent(jLabelTipoAgendamento)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(jComboBoxTipoAgendamento, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                                                    .addComponent(jComboBoxPlanoSaude, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(jTextFieldCodPlanoSaude, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(jButtonConsultaPlanoSaude, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                        .addGap(7, 7, 7))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelAgendamentoLayout.createSequentialGroup()
                                        .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                                                .addComponent(jComboBoxPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jTextFieldCodPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jButtonConsultaPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                                                .addComponent(jComboBoxMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jTextFieldCodMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jButtonConsultaMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanelAgendamentoLayout.setVerticalGroup(
            jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                        .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jTextFieldAgendamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelAgendamento))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabelPaciente)
                                        .addComponent(jComboBoxPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextFieldCodPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jButtonConsultaPaciente))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabelMedico)
                                        .addComponent(jComboBoxMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextFieldCodMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jButtonConsultaMedico))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabelStatus)
                                    .addComponent(jComboBoxStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(8, 8, 8)
                                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabelHorario)
                                    .addComponent(jFormattedTextFieldHorario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelTipoAgendamento)
                                    .addComponent(jComboBoxTipoAgendamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabelPlanoSaude)
                                        .addComponent(jComboBoxPlanoSaude, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextFieldCodPlanoSaude, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jButtonConsultaPlanoSaude))
                                .addGap(11, 11, 11)
                                .addComponent(jLabelObservacoes))
                            .addComponent(jCalendarAgendamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanelAgendamentoLayout.createSequentialGroup()
                                .addGroup(jPanelAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jButtonNovo)
                                    .addComponent(jButtonIncluir)
                                    .addComponent(jButtonAlterar))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabelCamposObrigatorios))))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE))
        );

        jTabbedPaneAgendamentoSecretaria.addTab("Agendamento", jPanelAgendamento);

        jTextFieldAgendamentoPagamento.setEnabled(false);

        jLabelAgendamentoPagamento.setText("Agendamento:");

        jLabelPacientePagamento.setText("Paciente:");

        jTextFieldPacientePagamento.setEnabled(false);

        jTextFieldCodPacientePagamento.setEnabled(false);

        javax.swing.GroupLayout jPanelPagamentoLayout = new javax.swing.GroupLayout(jPanelPagamento);
        jPanelPagamento.setLayout(jPanelPagamentoLayout);
        jPanelPagamentoLayout.setHorizontalGroup(
            jPanelPagamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPagamentoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelAgendamentoPagamento)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldAgendamentoPagamento, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabelPacientePagamento)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldPacientePagamento, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldCodPacientePagamento, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(666, Short.MAX_VALUE))
        );
        jPanelPagamentoLayout.setVerticalGroup(
            jPanelPagamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPagamentoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPagamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelAgendamentoPagamento)
                    .addComponent(jTextFieldAgendamentoPagamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelPacientePagamento)
                    .addComponent(jTextFieldPacientePagamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldCodPacientePagamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(516, Short.MAX_VALUE))
        );

        jTabbedPaneAgendamentoSecretaria.addTab("Pagamento", jPanelPagamento);

        javax.swing.GroupLayout jPanelCadastroAgendamentoLayout = new javax.swing.GroupLayout(jPanelCadastroAgendamento);
        jPanelCadastroAgendamento.setLayout(jPanelCadastroAgendamentoLayout);
        jPanelCadastroAgendamentoLayout.setHorizontalGroup(
            jPanelCadastroAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCadastroAgendamentoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPaneAgendamentoSecretaria, javax.swing.GroupLayout.PREFERRED_SIZE, 1245, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelCadastroAgendamentoLayout.setVerticalGroup(
            jPanelCadastroAgendamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCadastroAgendamentoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPaneAgendamentoSecretaria, javax.swing.GroupLayout.PREFERRED_SIZE, 580, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelCadastroAgendamento, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanelCadastroAgendamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonIncluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonIncluirActionPerformed
        if (jTextFieldCodPaciente.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "O campo paciente precisa ser preenchido!", "Atenção", JOptionPane.WARNING_MESSAGE);
            jComboBoxPaciente.grabFocus();
        } else if (jTextFieldCodMedico.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "O campo médico precisa ser preenchido!", "Atenção", JOptionPane.WARNING_MESSAGE);
            jComboBoxMedico.grabFocus();
        } else if (jFormattedTextFieldHorario.getText().equals("  :  ")) {
            JOptionPane.showMessageDialog(null, "O campo horário precisa ser preenchido!", "Atenção", JOptionPane.WARNING_MESSAGE);
            jFormattedTextFieldHorario.grabFocus();
        } else if (!"".equals(jTextFieldAgendamento.getText())) {
            JOptionPane.showMessageDialog(null, "Clique em Novo para poder incluir!", "Atenção", JOptionPane.WARNING_MESSAGE);
            jButtonNovo.grabFocus();
        } else if ("".equals(jTextFieldCodPlanoSaude.getText())) {
            JOptionPane.showMessageDialog(null, "O Plano de Saúde precisa ser preenchido!", "Atenção", JOptionPane.WARNING_MESSAGE);
            jComboBoxPlanoSaude.grabFocus();
        } else {
            validaagendamentocadastro();
        }
    }//GEN-LAST:event_jButtonIncluirActionPerformed

    private void jButtonIncluirMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonIncluirMouseDragged
        //        jButtonIncluir.setLocation(jButtonIncluir.getX() + evt.getX(), jButtonIncluir.getY() + evt.getY());
    }//GEN-LAST:event_jButtonIncluirMouseDragged

    private void jButtonConsultaMedicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConsultaMedicoActionPerformed
        jTabbedPaneAgendamentoSecretaria.setVisible(true);
        JPanel panel = new JPanel();
        panel.setName("Cadastro de Pessoa");
        panel.add(new Jp_CadastroPessoa());
        jTabbedPaneAgendamentoSecretaria.add(panel);
        jTabbedPaneAgendamentoSecretaria.setSelectedComponent(panel);

        int i = jTabbedPaneAgendamentoSecretaria.getSelectedIndex();
        jTabbedPaneAgendamentoSecretaria.setTabComponentAt(i, new ButtonTabComponent(jTabbedPaneAgendamentoSecretaria));
    }//GEN-LAST:event_jButtonConsultaMedicoActionPerformed

    private void jTableAgendamentoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableAgendamentoKeyReleased
        if (evt.getKeyCode() == 40) {
            if ("Atendido".equals(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getStatus_agendamento().trim())) {
                jTabbedPaneAgendamentoSecretaria.setEnabledAt(1, true);
            } else {
                jTabbedPaneAgendamentoSecretaria.setEnabledAt(1, false);
            }
        }
        if (evt.getKeyCode() == 38) {
            if ("Atendido".equals(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getStatus_agendamento().trim())) {
                jTabbedPaneAgendamentoSecretaria.setEnabledAt(1, true);
            } else {
                jTabbedPaneAgendamentoSecretaria.setEnabledAt(1, false);
            }
        }
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            if ("Atendido".equals(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getStatus_agendamento().trim())) {
                jTabbedPaneAgendamentoSecretaria.setEnabledAt(1, true);
            } else {
                jTabbedPaneAgendamentoSecretaria.setEnabledAt(1, false);
            }
        }
        /*
         String status = jTableAgendamento.getModel().getValueAt(jTableAgendamento.getSelectedRow(), 3).toString().trim();
         if (evt.getKeyCode() == KeyEvent.VK_ESCAPE && !"Atendido".equals(status)){
         carregardados();
         } else {
         jTabbedPaneAgendamento.setSelectedIndex(1); //Trocar de aba automaticamente
         }
         */
    }//GEN-LAST:event_jTableAgendamentoKeyReleased

    private void jTableAgendamentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableAgendamentoMouseClicked
        nom_paciente = ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getPaciente();
        try {
            listapaciente();
        } catch (SQLException ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (jTableAgendamento.getSelectedRow() != -1) {
            jComboBoxStatus.setEnabled(true);
        }

        if ("Atendido".equals(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getStatus_agendamento().trim())) {
            jTabbedPaneAgendamentoSecretaria.setEnabledAt(1, true);
        } else {
            jTabbedPaneAgendamentoSecretaria.setEnabledAt(1, false);
        }

        String status = jTableAgendamento.getModel().getValueAt(jTableAgendamento.getSelectedRow(), 3).toString().trim();
        //if (jTableAgendamento.getSelectedRow() != -1 && !"Atendido".equals(status)) {
        if (jTableAgendamento.getSelectedRow() != -1 && status.equals("Atendido")) {
            carregardados();
            jTabbedPaneAgendamentoSecretaria.setSelectedIndex(1); //Trocar de aba automaticamente
            carregardadospagamento();
        } else {
            carregardados();
        }
        /*
         else {
         limpardados();
         jTabbedPaneAgendamento.setSelectedIndex(1); //Trocar de aba automaticamente
         carregardadospagamento();
         }
         */
    }//GEN-LAST:event_jTableAgendamentoMouseClicked

    private void jButtonConsultaPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConsultaPacienteActionPerformed
        Fr_CadastroPessoa CP = new Fr_CadastroPessoa();
        CP.setVisible(true);
    }//GEN-LAST:event_jButtonConsultaPacienteActionPerformed

    private void jButtonAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAlterarActionPerformed
        if (jTextFieldAgendamento.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Selecione um agendamento para ser alterado!", "Atenção", JOptionPane.WARNING_MESSAGE);
            jTableAgendamento.grabFocus();
        } else if (jTextFieldCodPaciente.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "O campo paciente precisa ser preenchido!", "Atenção", JOptionPane.WARNING_MESSAGE);
            jComboBoxPaciente.grabFocus();
        } else if (jTextFieldCodMedico.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "O campo médico precisa ser preenchido!", "Atenção", JOptionPane.WARNING_MESSAGE);
            jComboBoxMedico.grabFocus();
            System.out.println(jFormattedTextFieldHorario.getText());
        } else if (jFormattedTextFieldHorario.getText().equals("  :  ")) {
            JOptionPane.showMessageDialog(null, "O campo horário precisa ser preenchido!", "Atenção", JOptionPane.WARNING_MESSAGE);
            jFormattedTextFieldHorario.grabFocus();
        } else if ("".equals(jTextFieldCodPlanoSaude.getText())) {
            JOptionPane.showMessageDialog(null, "O Plano de Saúde precisa ser preenchido!", "Atenção", JOptionPane.WARNING_MESSAGE);
            jComboBoxPlanoSaude.grabFocus();
        } else {
            altera();
        }
    }//GEN-LAST:event_jButtonAlterarActionPerformed

    private void jButtonNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNovoActionPerformed
        limpardados();
    }//GEN-LAST:event_jButtonNovoActionPerformed

    private void jComboBoxPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxPacienteActionPerformed
        if (jComboBoxPaciente.getSelectedIndex() > 0) {
            nom_paciente = jComboBoxPaciente.getSelectedItem().toString().trim();
            try {
                listapaciente();
                listapacientecod();
            } catch (SQLException ex) {
                Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParseException ex) {
                Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jComboBoxPacienteActionPerformed

    private void jComboBoxMedicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxMedicoActionPerformed
        if (jComboBoxMedico.getSelectedIndex() > 0) {
            try {
                listamedicocod();
            } catch (SQLException ex) {
                Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParseException ex) {
                Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jComboBoxMedicoActionPerformed

    private void jComboBoxPlanoSaudeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxPlanoSaudeActionPerformed
        if (jComboBoxPlanoSaude.getSelectedIndex() > 0) {
            try {
                listaplanosaudecod();
            } catch (SQLException ex) {
                Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParseException ex) {
                Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jComboBoxPlanoSaudeActionPerformed

    private void jCalendarAgendamentoPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jCalendarAgendamentoPropertyChange
        try {
            listaagendamento();
        } catch (SQLException ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jCalendarAgendamentoPropertyChange

    public void listapacientecod() throws SQLException, ParseException {
        CRUDPessoa CRUDPes = new CRUDPessoa();
        ListaPessoa = CRUDPes.getListaPessoaCod(jComboBoxPaciente.getSelectedItem().toString());
        mostraPacienteCod(ListaPessoa);
    }

    public void listamedicocod() throws SQLException, ParseException {
        CRUDPessoa CRUDPes = new CRUDPessoa();
        ListaPessoa = CRUDPes.getListaPessoaCod(jComboBoxMedico.getSelectedItem().toString());
        mostraMedicoCod(ListaPessoa);
    }

    public void listaplanosaudecod() throws SQLException, ParseException {
        CRUDPlanoSaude CRUDPlanoSaude = new CRUDPlanoSaude();
        ListaPlanoSaude = CRUDPlanoSaude.getListaPlanoSaudeCod(jComboBoxPlanoSaude.getSelectedItem().toString());
        mostraPlanoSaudeCod(ListaPlanoSaude);
    }

    public void listaagendamento() throws SQLException, ParseException {
        CRUDAgendamento CRUDAgend = new CRUDAgendamento();
        String data = new SimpleDateFormat("yyyy-MM-dd").format(jCalendarAgendamento.getDate());
        ListaAgendamento = CRUDAgend.getListaAgendamento(data);
        mostraAgendamento(ListaAgendamento);
    }

    public void listapaciente() throws SQLException, ParseException {
        CRUDPessoa CRUDPess = new CRUDPessoa();
        ListaPaciente = CRUDPess.getListaPessoaCod(nom_paciente);
        mostraPaciente(ListaPaciente);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAlterar;
    private javax.swing.JButton jButtonConsultaMedico;
    private javax.swing.JButton jButtonConsultaPaciente;
    private javax.swing.JButton jButtonConsultaPlanoSaude;
    private javax.swing.JButton jButtonIncluir;
    private javax.swing.JButton jButtonNovo;
    private com.toedter.calendar.JCalendar jCalendarAgendamento;
    private javax.swing.JComboBox jComboBoxMedico;
    private javax.swing.JComboBox jComboBoxPaciente;
    private javax.swing.JComboBox jComboBoxPlanoSaude;
    private javax.swing.JComboBox jComboBoxStatus;
    private javax.swing.JComboBox jComboBoxTipoAgendamento;
    private javax.swing.JFormattedTextField jFormattedTextFieldHorario;
    private javax.swing.JLabel jLabelAgendamento;
    private javax.swing.JLabel jLabelAgendamentoPagamento;
    private javax.swing.JLabel jLabelCPF;
    private javax.swing.JLabel jLabelCamposObrigatorios;
    private javax.swing.JLabel jLabelCel;
    private javax.swing.JLabel jLabelDenEnd;
    private javax.swing.JLabel jLabelEnd;
    private javax.swing.JLabel jLabelHorario;
    private javax.swing.JLabel jLabelMae;
    private javax.swing.JLabel jLabelMedico;
    private javax.swing.JLabel jLabelNomMae;
    private javax.swing.JLabel jLabelNomPaciente;
    private javax.swing.JLabel jLabelNomPai;
    private javax.swing.JLabel jLabelNumCPF;
    private javax.swing.JLabel jLabelNumCel;
    private javax.swing.JLabel jLabelNumTel;
    private javax.swing.JLabel jLabelObservacoes;
    private javax.swing.JLabel jLabelPaciente;
    private javax.swing.JLabel jLabelPacientePagamento;
    private javax.swing.JLabel jLabelPai;
    private javax.swing.JLabel jLabelPlanoSaude;
    private javax.swing.JLabel jLabelStatus;
    private javax.swing.JLabel jLabelTel;
    private javax.swing.JLabel jLabelTipoAgendamento;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelAgendamento;
    private javax.swing.JPanel jPanelCadastroAgendamento;
    private javax.swing.JPanel jPanelPagamento;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPaneAgendamentoSecretaria;
    public javax.swing.JTable jTableAgendamento;
    private javax.swing.JTextArea jTextAreaObservacoes;
    private javax.swing.JTextField jTextFieldAgendamento;
    public javax.swing.JTextField jTextFieldAgendamentoPagamento;
    private javax.swing.JTextField jTextFieldCodMedico;
    private javax.swing.JTextField jTextFieldCodPaciente;
    private javax.swing.JTextField jTextFieldCodPacientePagamento;
    private javax.swing.JTextField jTextFieldCodPlanoSaude;
    private javax.swing.JTextField jTextFieldPacientePagamento;
    // End of variables declaration//GEN-END:variables

    public void limpardados() {
        jFormattedTextFieldHorario.setText(null);
        jTextFieldAgendamento.setText(null);
        jTextFieldCodPaciente.setText(null);
        jTextFieldCodMedico.setText(null);
        jTextFieldCodPlanoSaude.setText(null);
        jTextAreaObservacoes.setText(null);
        jComboBoxStatus.setSelectedIndex(0);
        jComboBoxStatus.setEnabled(false);
        jComboBoxTipoAgendamento.setSelectedIndex(0);
        try {
            listaPaciente();
            listaMedico();
            listaPlanoSaude();
        } catch (Exception ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        }
        jTabbedPaneAgendamentoSecretaria.setEnabledAt(1, false);
        jLabelNomMae.setText(null);
        jLabelNomMae.setText(null);
        jLabelDenEnd.setText(null);
        jLabelNumTel.setText(null);
        jLabelNumCel.setText(null);
        jLabelNumCPF.setText(null);
        jLabelNomPaciente.setText(null);
    }

    public void carregardados() {
        jTextFieldAgendamento.setText(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getCod_agendamento().toString());
        jComboBoxPaciente.setSelectedItem(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getPaciente());
        jTextFieldCodPaciente.setText(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getCod_paciente());
        jTextFieldCodMedico.setText(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getCod_medico());
        jTextFieldCodPlanoSaude.setText(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getCod_plano_saude().toString());
        jFormattedTextFieldHorario.setText(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getHora_agendamento().toString());
        jTextAreaObservacoes.setText(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getObservacao());

        if (ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getStatus_agendamento().trim().equals("Em Aberto")) {
            jComboBoxStatus.setSelectedIndex(0);
        }

        if (ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getStatus_agendamento().trim().equals("Em Atendimento")) {
            jComboBoxStatus.setSelectedIndex(1);
        }

        if (ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getStatus_agendamento().trim().equals("Cancelado")) {
            jComboBoxStatus.setSelectedIndex(2);
        }

        jComboBoxPaciente.setSelectedIndex(0);
        jComboBoxMedico.setSelectedIndex(0);
        jComboBoxPlanoSaude.setSelectedIndex(0);
        jLabelNomPaciente.setText(nom_paciente);
    }

    public void carregardadospagamento() {
        jTextFieldAgendamentoPagamento.setText(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getCod_agendamento().toString());
        jTextFieldPacientePagamento.setText(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getPaciente());
        jTextFieldCodPacientePagamento.setText(ListaAgendamento.get(jTableAgendamento.getSelectedRow()).getCod_paciente());
    }

    private void AlinhaColunaJTable() {
        int col = 0;
        jTableAgendamento.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //barra horizontal na JTable 
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(100);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(110);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(60);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(45);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(230);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(230);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(120);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(120);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(80);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(80);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(80);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(130);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(230);
        jTableAgendamento.getColumnModel().getColumn(col++).setPreferredWidth(350);
    }

    private void listaPaciente() throws Exception {
        jComboBoxPaciente.removeAllItems();

        BeanPessoa Seleciona = new BeanPessoa();
        Seleciona.setNom_pessoa("Selecione um Paciente");
        jComboBoxPaciente.addItem(Seleciona);

        CRUDPessoa CRUDPess = new CRUDPessoa();
        List<BeanPessoa> listastatus;

        jComboBoxPaciente.setEnabled(true);
        listastatus = CRUDPess.getListaPaciente();

        Iterator<BeanPessoa> itpaciente = listastatus.iterator();
        while (itpaciente.hasNext()) {
            BeanPessoa BP = itpaciente.next();
            jComboBoxPaciente.addItem(BP);
        }
    }

    private void listaMedico() throws Exception {
        jComboBoxMedico.removeAllItems();

        BeanPessoa Seleciona = new BeanPessoa();
        Seleciona.setNom_pessoa("Selecione um Médico");
        jComboBoxMedico.addItem(Seleciona);

        CRUDPessoa CRUDPess = new CRUDPessoa();
        List<BeanPessoa> listastatus;

        jComboBoxMedico.setEnabled(true);
        listastatus = CRUDPess.getListaMedico();

        Iterator<BeanPessoa> itpaciente = listastatus.iterator();
        while (itpaciente.hasNext()) {
            BeanPessoa BP = itpaciente.next();
            jComboBoxMedico.addItem(BP);
        }
    }

    private void listaPlanoSaude() throws Exception {
        jComboBoxPlanoSaude.removeAllItems();

        BeanPlanoSaude Seleciona = new BeanPlanoSaude();
        Seleciona.setDen_plano_saude("Selecione um Plano de Saúde");
        jComboBoxPlanoSaude.addItem(Seleciona);

        CRUDPlanoSaude CRUDPlanSaude = new CRUDPlanoSaude();
        List<BeanPlanoSaude> listaplanosaude;

        jComboBoxPlanoSaude.setEnabled(true);
        listaplanosaude = CRUDPlanSaude.getListaPlanoSaude();

        Iterator<BeanPlanoSaude> itpaciente = listaplanosaude.iterator();
        while (itpaciente.hasNext()) {
            BeanPlanoSaude BP = itpaciente.next();
            jComboBoxPlanoSaude.addItem(BP);
        }
    }

    private void mostraPacienteCod(List<BeanPessoa> ListaPessoa) {
        for (int i = 0; i < ListaPessoa.size(); i++) {
            jTextFieldCodPaciente.setText(ListaPessoa.get(i).getCod_pessoa());
        }
    }

    private void mostraMedicoCod(List<BeanPessoa> ListaPessoa) {
        for (int i = 0; i < ListaPessoa.size(); i++) {
            jTextFieldCodMedico.setText(ListaPessoa.get(i).getCod_pessoa());
        }
    }

    private void mostraPlanoSaudeCod(List<BeanPlanoSaude> ListaPlanoSaude) {
        for (int i = 0; i < ListaPlanoSaude.size(); i++) {
            jTextFieldCodPlanoSaude.setText(ListaPlanoSaude.get(i).getCod_plano_saude().toString());
        }
    }

    public void validaagendamentocadastro() {
        String str = jFormattedTextFieldHorario.getText();
        SimpleDateFormat formatador = new SimpleDateFormat("HH:mm");
        Date hora = null;
        try {
            hora = formatador.parse(str);
        } catch (ParseException ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        }
        Time time = new Time(hora.getTime());
        String data = new SimpleDateFormat("yyyy-MM-dd").format(jCalendarAgendamento.getDate());

        String sql = "select hora_agendamento from agendamento where hora_agendamento = ? and dt_agendamento = ?";
        try {
            PreparedStatement stmt = this.conexao.prepareStatement(sql);
            stmt.setTime(1, time);
            stmt.setString(2, data);
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Já existe um agendamento para esse horário!", "Atenção", JOptionPane.WARNING_MESSAGE);
                jFormattedTextFieldHorario.setText(null);
                jFormattedTextFieldHorario.grabFocus();
            } else {
                cadastro();
            }
        } catch (SQLException ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void cadastro() {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
            //SimpleDateFormat formato = new SimpleDateFormat("hh:mm");
            BeanAgendamento BA = new BeanAgendamento();

            //BP.setDt_cadastro(formatter.parse(jTextFieldCadastro.getText().replace("/", ".")));

            //BA.setCod_agendamento(Integer.valueOf(jTextFieldAgendamento.getText()));
            BA.setDt_agendamento(jCalendarAgendamento.getDate());
            BA.setCod_paciente(jTextFieldCodPaciente.getText());
            BA.setCod_medico(jTextFieldCodMedico.getText());
            if (jComboBoxStatus.getSelectedItem().toString().equals("Em Aberto")) {
                BA.setStatus_agendamento("Em Aberto");
            }
            if (jComboBoxStatus.getSelectedItem().toString().equals("Em Atendimento")) {
                BA.setStatus_agendamento("Em Atendimento");
            }
            if (jComboBoxStatus.getSelectedItem().toString().equals("Atendido")) {
                BA.setStatus_agendamento("Atendido");
            }
            if (jComboBoxStatus.getSelectedItem().toString().equals("Cancelado")) {
                BA.setStatus_agendamento("Cancelado");
            }
            BA.setObservacao(jTextAreaObservacoes.getText());
            BA.setCod_plano_saude(Integer.valueOf(jTextFieldCodPlanoSaude.getText()));

            String str = jFormattedTextFieldHorario.getText();
            SimpleDateFormat formatador = new SimpleDateFormat("HH:mm");
            Date data = null;
            try {
                data = formatador.parse(str);
            } catch (ParseException ex) {
                Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
            }
            Time time = new Time(data.getTime());

            BA.setHora_agendamento(time);

            CRUDAgendamento dao = new CRUDAgendamento();
            dao.adiciona(BA);
            limpardados();
            try {
                listaagendamento();
            } catch (ParseException ex) {
                Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void altera() {
        BeanAgendamento BA = new BeanAgendamento();
        BA.setCod_agendamento(Integer.valueOf(jTextFieldAgendamento.getText()));
        BA.setDt_agendamento(jCalendarAgendamento.getDate());
        BA.setCod_paciente(jTextFieldCodPaciente.getText());
        BA.setCod_medico(jTextFieldCodMedico.getText());
        if (jComboBoxStatus.getSelectedItem().toString().equals("Em Aberto")) {
            BA.setStatus_agendamento("Em Aberto");
        }
        if (jComboBoxStatus.getSelectedItem().toString().equals("Em Atendimento")) {
            BA.setStatus_agendamento("Em Atendimento");
        }
        if (jComboBoxStatus.getSelectedItem().toString().equals("Atendido")) {
            BA.setStatus_agendamento("Atendido");
        }
        if (jComboBoxStatus.getSelectedItem().toString().equals("Cancelado")) {
            BA.setStatus_agendamento("Cancelado");
        }
        BA.setObservacao(jTextAreaObservacoes.getText());
        BA.setCod_plano_saude(Integer.valueOf(jTextFieldCodPlanoSaude.getText()));

        String str = jFormattedTextFieldHorario.getText();
        SimpleDateFormat formatador = new SimpleDateFormat("HH:mm");
        Date data = null;
        try {
            data = formatador.parse(str);
        } catch (ParseException ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        }
        Time time = new Time(data.getTime());

        BA.setHora_agendamento(time);
        CRUDAgendamento dao = null;

        try {
            dao = new CRUDAgendamento();
            limpardados();
            try {
                listaagendamento();
            } catch (ParseException ex) {
                Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            dao.alteraSecretaria(BA);
            limpardados();
            try {
                listaagendamento();
            } catch (ParseException ex) {
                Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void mostraAgendamento(List<BeanAgendamento> ListaAgendamento) {
        if (TMAgendamento == null) {
            JOptionPane.showMessageDialog(null, "teste");
        } else {
            while (TMAgendamento.getRowCount() > 0) {
                TMAgendamento.removeRow(0);
            }
            if (ListaAgendamento.size() < 1) {
                //JOptionPane.showMessageDialog(null, "teste2");
            } else {
                String[] linha = new String[]{null, null};
                for (int i = 0; i < ListaAgendamento.size(); i++) {
                    TMAgendamento.addRow(linha);
                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getCod_agendamento(), i, 0);

                    String dt_agendamento = ListaAgendamento.get(i).getDt_agendamento().toString();
                    String str_dt_agendamento = dt_agendamento;
                    String[] array_dt_agendamento = str_dt_agendamento.split("-");
                    String dt_agendamento_alter = array_dt_agendamento[2] + "/" + array_dt_agendamento[1] + "/" + array_dt_agendamento[0];
                    TMAgendamento.setValueAt(dt_agendamento_alter, i, 1);

                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getHora_agendamento(), i, 2);
                    //jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("D:\\programacao\\JavaApplication2\\src\\Imagens\\Incluir.png")));
                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getStatus_agendamento().trim(), i, 3);                    

                    //jTableAgendamento.setForeground(Color.yellow);


                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getMedico(), i, 4);
                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getPaciente(), i, 5);
                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getDen_plano_saude(), i, 6);
                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getTelefone(), i, 7);

                    String dt_nasc = ListaAgendamento.get(i).getDt_nasc().toString();
                    String str_dt_nasc = dt_nasc;
                    String[] array_dt_nasc = str_dt_nasc.split("-");
                    String dt_nasc_alter = array_dt_nasc[2] + "/" + array_dt_nasc[1] + "/" + array_dt_nasc[0];
                    TMAgendamento.setValueAt(dt_nasc_alter, i, 8);

                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getIdade(), i, 9);

                    if ("M".equals(ListaAgendamento.get(i).getSexo())) {
                        TMAgendamento.setValueAt("Masculino", i, 10);
                    } else {
                        TMAgendamento.setValueAt("Feminino", i, 10);
                    }

                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getCidade(), i, 11);
                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getEndereco().trim(), i, 12);
                    TMAgendamento.setValueAt(ListaAgendamento.get(i).getObservacao(), i, 13);

                    jTableAgendamento.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
                        public Component getTableCellRendererComponent(JTable table, Object value,
                                boolean isSelected, boolean hasFocus, int row, int column) {
                            super.getTableCellRendererComponent(table, value, isSelected,
                                    hasFocus, row, column);
                            Object coluna = table.getValueAt(row, 3);

                            if (column == 3 && coluna.equals("Atendido")) {
                                setBackground(Color.green);
                                setForeground(Color.green);
                            } else {
                                if (column == 3 && coluna.equals("Cancelado")) {
                                    setBackground(Color.red);
                                    setForeground(Color.red);
                                } else {
                                    if (column == 3 && coluna.equals("Em Aberto")) {
                                        setBackground(Color.yellow);
                                        setForeground(Color.yellow);
                                    } else {
                                        if (column == 3 && coluna.equals("Em Atendimento")) {
                                            setBackground(Color.blue);
                                            setForeground(Color.blue);
                                        } else {
                                            setForeground(Color.black);
                                            setBackground(null);
                                        }
                                    }
                                }
                            }
                            return this;
                        }
                    });
                }
            }
        }
    }

    private void mostraPaciente(List<BeanPessoa> ListaPaciente) {
        for (int i = 0; i < ListaPaciente.size(); i++) {
            jLabelDenEnd.setText(ListaPaciente.get(i).getEndereco().trim());
            jLabelNumTel.setText(ListaPaciente.get(i).getTelefone().trim());
            jLabelNumCel.setText(ListaPaciente.get(i).getCelular().trim());
            jLabelNumCPF.setText(ListaPaciente.get(i).getCpf().trim());
            jLabelNomPaciente.setText(nom_paciente);
        }
    }

    public void run() {

        int delay = 5000;   // delay de 5 seg.
        int interval = 1000;  // intervalo de 1 seg.
        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                try {
                    listaagendamento();
                } catch (SQLException ex) {
                    Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ParseException ex) {
                    Logger.getLogger(Jp_CadastroAgendamentoSecretaria.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }, delay, interval);
    }
}